package com.valveinspectionsys.order.db;

import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.valveinspectionsys.base.BaseDao;
import com.valveinspectionsys.dbhelp.DbTableName;
import com.valveinspectionsys.povo.DeviceVo;
import com.valveinspectionsys.povo.EngineeringOrderPoVo;
import com.valveinspectionsys.povo.SpinnerVo;

/**
 * 工程单数据交互
 * @author King
 * 2013-5-6
 */
public class OrderDao extends BaseDao {
    
    private SQLiteDatabase db;
    public OrderDao(Context context, String SqliteName) {
	super(context, SqliteName);
	this.db=super.getDb();
    }
  
    /**
     * 查询工程单类型
     * 
     * @return
     */
    public List<SpinnerVo> queryOrderType() {
	List<SpinnerVo> list = new ArrayList<SpinnerVo>();
	String sql = "select * from type where status='2'";
	Cursor cursor = db.rawQuery(sql, null);
          SpinnerVo  sp=new SpinnerVo("0", "请选择");
         list.add(sp);
	while (cursor.moveToNext()) {
	    SpinnerVo type = new SpinnerVo(cursor.getString(cursor.getColumnIndex("number")), cursor.getString(cursor.getColumnIndex("name")));	  	   
	    list.add(type);
	}

	if (cursor != null) {
	    cursor.close();
	}
	return list;
    }

    /**
     * 添加工程单
     * @param value
     * @return
     */
  public int InsertOrder(){
      ContentValues value=new ContentValues();
      value.put("ordertype", "-1");
      int id=0;
      long k=db.insert(DbTableName.ORDER,null, value);
      System.out.println("------kkkkk------"+k);
      if(k>0){
	  String sql="select max(_id) as id from engineeringOrder  ";
	  Cursor  cursor=db.rawQuery(sql, null);
	  System.out.println("-----11111------"+cursor.getCount());
	  if(cursor.moveToNext()){
	      id=cursor.getInt(cursor.getColumnIndex("id"));
	      System.out.println("---id-----------"+id);
	  }
	  if(cursor!=null){
	      cursor.close();
	  }
      }
    return id;
  }
  
  /**
   * 修改工程单
   * @param value
   * @param id
   * @return
   */
  public boolean modifyOrder(ContentValues value,String id){
      boolean b=false;
      int k=db.update(DbTableName.ORDER, value, "_id='"+id+"'",null);
      if(k>0){
	  return true;
      }
    return b;
  }
  
  /**
   * 删除一个工程单
   * @param id
   * @return
   */
  public boolean delectOrder(String id){
      boolean b=false;
    int k=  db.delete(DbTableName.ORDER, "_id='"+id+"'", null);
    if(k>0){
	return true;
    }
    return b;
  }
  
  /**
   * 添加后删除
   * @param id
   * @return
   */
  public boolean delectOrderNoAdd(String id){
      String sql="select ordertype from engineeringOrder where _id='"+id+"'";
      Cursor  cursor=db.rawQuery(sql, null);
      if(cursor.moveToNext()){
	  String type=cursor.getString(cursor.getColumnIndex("ordertype"));
	  if(type.equals("-1")){
	      db.delete(DbTableName.ORDER, "_id='"+id+"'", null);
	  }
      }
      if(cursor!=null){
	  cursor.close();
      }
    return false;
      
  }
  
  /**
   * 读卡查询一个设备信息
   * @param rfid
   * @param deviceid
   * @return
   */
  public DeviceVo queryDevice(String rfid,String deviceid){
      DeviceVo  device=null;
      String sql="select * from deviceinfo where 0=0 ";
      if(rfid!=null){
	  sql=sql+" and RFIDnumber='"+rfid+"'";
      }
      if(deviceid!=null){
	  sql=sql+" and deviceid='"+deviceid+"'";
      }
      Cursor cursor=db.rawQuery(sql, null);
    
	 if(cursor.moveToNext()){
	     device=new DeviceVo();
	     device.setId(cursor.getString(cursor.getColumnIndex("_id")));
	     device.setDeviceId(cursor.getString(cursor.getColumnIndex("deviceid")));
	     device.setUseStatus(cursor.getString(cursor.getColumnIndex("useStatus")));
	     device.setOpenStatus(cursor.getString(cursor.getColumnIndex("openStatus")));
	     device.setDeviceNumber(cursor.getString(cursor.getColumnIndex("devicenumber")));
	     device.setDeviceAdds(cursor.getString(cursor.getColumnIndex("deviceadds")));
	     device.setDeviceCaliber(cursor.getString(cursor.getColumnIndex("devicecaliber")));
	     device.setDeviceType(cursor.getString(cursor.getColumnIndex("devicetype")));
	     device.setDevicePressure(cursor.getString(cursor.getColumnIndex("devicePressure")));
	     device.setInspectionCycle(cursor.getString(cursor.getColumnIndex("inspectionCycle")));
	     device.setInstallationDate(cursor.getString(cursor.getColumnIndex("InstallationDate")));
	     device.setLongitude(cursor.getDouble(cursor.getColumnIndex("longitude")));
	     device.setLatitude(cursor.getDouble(cursor.getColumnIndex("latitude")));
	     device.setMaintenanceCycle(cursor.getString(cursor.getColumnIndex("maintenanceCycle")));
	     device.setProducers(cursor.getString(cursor.getColumnIndex("producers")));
	     device.setProductionDate(cursor.getString(cursor.getColumnIndex("productionDate")));
	     device.setRfidNumber(cursor.getString(cursor.getColumnIndex("RFIDnumber")));
  
	 }
	
	 if(cursor!=null){
	     cursor.close();
	 }
    return device;
  }
  
  /**
   * 查询工程单
   * @param orderid
   * @return
   */
  public List<EngineeringOrderPoVo> queryOrder( String orderid){

	List<EngineeringOrderPoVo> list = new ArrayList<EngineeringOrderPoVo>();

	String sql = "select e.[_id],e.[status],e.[deviceid],e.[ordertype],e.[ordername],e.[desc],e.[isimg],e.[videopath],"
		+ "e.[soundpath],t.[name] from engineeringorder e,type t where e.[ordertype]=t.[number] and t.[status]='2'";

	if (orderid != null) {
	    sql = sql + " and  e.[_id]='" + orderid + "'";
	}
	sql = sql + " order by e.[_id] desc";
	System.out.println(sql);
	Cursor cursor = db.rawQuery(sql, null);

	while (cursor.moveToNext()) {
	    EngineeringOrderPoVo order = new EngineeringOrderPoVo();
	    order.setDesc(cursor.getString(cursor.getColumnIndex("desc")));
	    order.setTypenumber(cursor.getString(cursor.getColumnIndex("ordertype")));
	    order.setName(cursor.getString(cursor.getColumnIndex("ordername")));
	    order.setWaternumber(cursor.getString(cursor.getColumnIndex("deviceid")));
            order.setIshaveImg(cursor.getString(cursor.getColumnIndex("isimg")).equals("1"));
	    order.setStatus(cursor.getString(cursor.getColumnIndex("status")));
	    order.setVideopath(cursor.getString(cursor.getColumnIndex("videopath")));
	  
	    order.setSoundpath(cursor.getString(cursor.getColumnIndex("soundpath")));
	    order.setTypename(cursor.getString(cursor.getColumnIndex("name")));
	    order.setId(cursor.getString(cursor.getColumnIndex("_id")));
	    System.out.println("------viceo-------"+order.getVideopath());
	    System.out.println("-------sound--------"+order.getSoundpath());
	    list.add(order);

	}
	if (cursor != null) {
	    cursor.close();
	}

	return list;

  }
  
  /**
   * 查询未上传工程单数
   * 
   * @return
   */
  public int queryOrderUpload() {
      int i=0;
	String sql = "select count(_id) as total from engineeringOrder where status='0'";
	Cursor cursor = db.rawQuery(sql, null);
	if(cursor.moveToNext()){
	    i=cursor.getInt(cursor.getColumnIndex("total"));
	}
	if (cursor != null) {
	    cursor.close();
	}
	return i;

  }
  
  /**
   * 查询工程单总数据
   * 
   * @return
   */
  public int queryOrderTotal() {
        int i=0;
	String sql = "Select count(_id) as total  From engineeringOrder ";
	Cursor cursor = db.rawQuery(sql, null);
	if(cursor.moveToNext()){
	    i=cursor.getInt(cursor.getColumnIndex("total"));
	}
	if (cursor != null) {
	    cursor.close();
	}
	System.out.println("------iiii-----"+i);
	return i;
 
  }
  
  /**
   * 查询所需要上传的工程单
   * 
   * @return
   */
  public List<EngineeringOrderPoVo> queryUpload(String orderid) {

	List<EngineeringOrderPoVo> list = new ArrayList<EngineeringOrderPoVo>();
	String sql = "select * from engineeringorder e where e.[status]='0'";
	if (orderid != null) {
	    sql = sql + " and e.[_id]='" + orderid + "'";
	}
	Cursor cursor = db.rawQuery(sql, null);
	Cursor cur = null;
	while (cursor.moveToNext()) {
	    EngineeringOrderPoVo order = new EngineeringOrderPoVo();
	    order.setDesc(cursor.getString(cursor.getColumnIndex("desc")));
	    order.setId(cursor.getString(cursor.getColumnIndex("_id")));
	    order.setName(cursor.getString(cursor.getColumnIndex("ordername")));
	    order.setSoundpath(cursor.getString(cursor.getColumnIndex("soundpath")));
	    order.setTypenumber(cursor.getString(cursor.getColumnIndex("ordertype")));
	    order.setWaternumber(cursor.getString(cursor.getColumnIndex("deviceid")));
	    order.setVideopath(cursor.getString(cursor.getColumnIndex("videopath")));
	    order.setLatitude(cursor.getDouble(cursor.getColumnIndex("latitude")));
	    order.setLongitude(cursor.getDouble(cursor.getColumnIndex("longitude")));
	    if(cursor.getString(cursor.getColumnIndex("isimg")).equals("1")){
		  String reoouseSQL = "select * from " + DbTableName.RESOURCES + " where otherid='" + order.getId() + "' and type='1'" + "and status='-1'";
		    cur = db.rawQuery(reoouseSQL, null);
		    List<String> listimg = new ArrayList<String>();
		    while (cur.moveToNext()) {

			listimg.add(cur.getString(cur.getColumnIndex("path")));
		    }
		  
		    order.setImagepath(listimg);
	    }
	  
	    list.add(order);
	}
	  if (cur != null) {
			cur.close();
		    }
	if (cursor != null) {
	    cursor.close();
	}

	return list;

  }

}
